package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.UserData;
import com.xijia.wuyouwallet.pref.PrefFactory;
import com.xijia.wuyouwallet.utils.CharCheckUtil;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.SharedpreferncesUtil;
import com.xijia.wuyouwallet.utils.StringUtils;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/14 0014.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private ImageView login_back,login_remember;
    private EditText login_phone,login_paswd;
    private TextView login_forgetPaswd,login_login,login_register;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }
    private void initView(){
        login_back = (ImageView) findViewById(R.id.login_back);
        login_back.setOnClickListener(this);
        login_remember = (ImageView) findViewById(R.id.login_remember);
        login_remember.setOnClickListener(this);
        login_phone = (EditText) findViewById(R.id.login_phone);
        login_paswd = (EditText) findViewById(R.id.login_paswd);
        login_forgetPaswd = (TextView) findViewById(R.id.login_forgetPaswd);
        login_forgetPaswd.setOnClickListener(this);
        login_login = (TextView) findViewById(R.id.login_login);
        login_login.setOnClickListener(this);
        login_register = (TextView) findViewById(R.id.login_register);
        login_register.setOnClickListener(this);
        String phone = SharedpreferncesUtil.getString("user_phone", "", LoginActivity.this);
        login_phone.setText(phone);
    }

    /*public static void finsh (){
        LoginActivity.finsh();
    }*/

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_back:
                finish();
                break;
            case R.id.login_register:
                jumpToPage(RegisterActivity.class);
                finish();
                break;
            case R.id.login_remember:

                break;
            case R.id.login_login:
                login();
                break;
            case R.id.login_forgetPaswd:
                jumpToPage(SearchLoginPsdActivity.class);
                break;
        }
    }
    private void login(){
        final String phone = login_phone.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        final String pswd = login_paswd.getText().toString().trim();
        if (pswd.isEmpty()){
            showToast("请输密码");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("login_name",phone);
        map.put("login_passwd",pswd);
        map.put("device_number", StringUtils.getid(this));
        showProgressDialog(true);
        XUtil.Post(UrlConfig.LOGIN_URL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                MyLogger.wLog().e("logon result="+result);
                MyApp.userData = JSONObject.parseObject(result,UserData.class);
                if (MyApp.userData.getCode()==200){
                    showToast("登陆成功");
                    PrefFactory.getUserPref().setSelfMotionLoginT();
                    PrefFactory.getUserPref().putLogonPwd(pswd);
                    PrefFactory.getUserPref().putLogonPhone(phone);
                    finish();
                    jumpToPage(HomeActivity.class);
                }else {
                    showToast(MyApp.userData.getMsg());
                    MyApp.userData = null;
                    return;
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                 showToast("登录失败，请检查网络连接");
            }
        });
    }

}
